﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using WinRTFramework.Common;
using WinRTFramework.Interfaces.Navigation;
using WinRTFramework.Interfaces.Navigation.ViewModel;

namespace SeriesManager.ViewModel
{
    public class ViewModelBase : BindableBase, IRequireNavigationService, ICareAboutNavigationFlow, ICareAboutSuspendResume
    {
        public INavigationService NavigationService
        {
            set;
            protected get;
        }

        public ICommand BackCommand
        {
            get;
            private set;
        }

        public bool NavigationBarIsOpen
        {
            get
            {
                return AppX.Instance.NavigationBarIsOpen;
            }
            set
            {
                AppX.Instance.NavigationBarIsOpen = value;
            }
        }

        public ViewModelBase()
        {
            AppX.Instance.PropertyChanged += Instance_PropertyChanged;
            this.BackCommand = new DelegateCommand<ItemClickEventArgs>(OnBackExecuted, OnBackCanExecute);
        }

        private void Instance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "NavigationBarIsOpen")
            {
                OnPropertyChanged("NavigationBarIsOpen");
            }
        }

        public void Resumed(object navigationParameter, ResumePreviousAppState previousAppState)
        {
            if (previousAppState == ResumePreviousAppState.Terminated)
            {
                this.OnNavigatedTo(navigationParameter);
            }
        }

        public virtual void Suspending()
        {
        }

        public virtual void OnNavigatedFrom()
        {
        }

        public virtual void OnNavigatedTo(object navigationParameter)
        {
        }

        private void OnBackExecuted(ItemClickEventArgs args)
        {
            this.NavigationService.GoBackCommand.Execute(args);
        }

        private bool OnBackCanExecute(ItemClickEventArgs args)
        {
            return this.NavigationService.GoBackCommand.CanExecute(args);
        }

        public virtual void OnNavigatingFrom()
        {
        }
    }
}